package com.hqyj.finance.server.bill.financebill.controller;

import com.hqyj.finance.server.bill.financebill.json.R;
import com.hqyj.finance.server.bill.financebill.service.QiniuService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("qiniu")
public class QiniuController {

    @Resource
    private QiniuService qiniuService;

    @PostMapping(value = "/upload")
    public R uploadImage(@RequestParam("file") MultipartFile file, HttpServletRequest request) {

        if (file.isEmpty()) {
            return R.fail("上传出现错误！");
        }
        try {
            String fileUrl = qiniuService.saveImage(file);
            return R.success("文件上传成功！", fileUrl);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

}
